/**
 * 存储工具函数
 */

/**
 * 设置localStorage
 * @param {string} key - 键
 * @param {any} value - 值
 * @returns {boolean} 是否设置成功
 */
export function setLocalStorage(key, value) {
  try {
    const serializedValue = JSON.stringify(value);
    localStorage.setItem(key, serializedValue);
    return true;
  } catch (error) {
    console.error('设置localStorage失败:', error);
    return false;
  }
}

/**
 * 获取localStorage
 * @param {string} key - 键
 * @param {any} defaultValue - 默认值
 * @returns {any} 值
 */
export function getLocalStorage(key, defaultValue = null) {
  try {
    const item = localStorage.getItem(key);
    return item ? JSON.parse(item) : defaultValue;
  } catch (error) {
    console.error('获取localStorage失败:', error);
    return defaultValue;
  }
}

/**
 * 删除localStorage
 * @param {string} key - 键
 * @returns {boolean} 是否删除成功
 */
export function removeLocalStorage(key) {
  try {
    localStorage.removeItem(key);
    return true;
  } catch (error) {
    console.error('删除localStorage失败:', error);
    return false;
  }
}

/**
 * 清空localStorage
 * @returns {boolean} 是否清空成功
 */
export function clearLocalStorage() {
  try {
    localStorage.clear();
    return true;
  } catch (error) {
    console.error('清空localStorage失败:', error);
    return false;
  }
}

/**
 * 设置sessionStorage
 * @param {string} key - 键
 * @param {any} value - 值
 * @returns {boolean} 是否设置成功
 */
export function setSessionStorage(key, value) {
  try {
    const serializedValue = JSON.stringify(value);
    sessionStorage.setItem(key, serializedValue);
    return true;
  } catch (error) {
    console.error('设置sessionStorage失败:', error);
    return false;
  }
}

/**
 * 获取sessionStorage
 * @param {string} key - 键
 * @param {any} defaultValue - 默认值
 * @returns {any} 值
 */
export function getSessionStorage(key, defaultValue = null) {
  try {
    const item = sessionStorage.getItem(key);
    return item ? JSON.parse(item) : defaultValue;
  } catch (error) {
    console.error('获取sessionStorage失败:', error);
    return defaultValue;
  }
}

/**
 * 删除sessionStorage
 * @param {string} key - 键
 * @returns {boolean} 是否删除成功
 */
export function removeSessionStorage(key) {
  try {
    sessionStorage.removeItem(key);
    return true;
  } catch (error) {
    console.error('删除sessionStorage失败:', error);
    return false;
  }
}

/**
 * 清空sessionStorage
 * @returns {boolean} 是否清空成功
 */
export function clearSessionStorage() {
  try {
    sessionStorage.clear();
    return true;
  } catch (error) {
    console.error('清空sessionStorage失败:', error);
    return false;
  }
}

/**
 * 存储管理器
 */
export class StorageManager {
  constructor(storage = localStorage) {
    this.storage = storage;
  }

  set(key, value) {
    try {
      const serializedValue = JSON.stringify(value);
      this.storage.setItem(key, serializedValue);
      return true;
    } catch (error) {
      console.error('存储失败:', error);
      return false;
    }
  }

  get(key, defaultValue = null) {
    try {
      const item = this.storage.getItem(key);
      return item ? JSON.parse(item) : defaultValue;
    } catch (error) {
      console.error('获取存储失败:', error);
      return defaultValue;
    }
  }

  remove(key) {
    try {
      this.storage.removeItem(key);
      return true;
    } catch (error) {
      console.error('删除存储失败:', error);
      return false;
    }
  }

  clear() {
    try {
      this.storage.clear();
      return true;
    } catch (error) {
      console.error('清空存储失败:', error);
      return false;
    }
  }

  has(key) {
    return this.storage.getItem(key) !== null;
  }

  keys() {
    const keys = [];
    for (let i = 0; i < this.storage.length; i++) {
      keys.push(this.storage.key(i));
    }
    return keys;
  }

  size() {
    return this.storage.length;
  }
}

// 创建默认实例
export const localStore = new StorageManager(localStorage);
export const sessionStore = new StorageManager(sessionStorage);
